package com.bnyy.message.bean.chat;

import com.bnyy.common.bean.UserInfo;
import com.bnyy.message.bean.Contact;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.bean.ContactUser;
import com.bnyy.message.bean.Message;
import com.bnyy.message.bean.chat.message_data.MessageData;
import com.bnyy.message.enums.MessageType;
import com.bnyy.message.enums.ChatMessageStatus;
import com.google.gson.annotations.SerializedName;

import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

import java.util.ArrayList;
import java.util.Random;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/1
 **/
@Table(name = "chat_message")
public class ChatMessage extends Message {

    public interface ColumnName {
        String CHAT_ID = "chat_id";
        String DURATION = "duration";
        String PLAYED = "played";
        String SEND_STATUS = "send_status";
        String READ = "read";
        String STRANGER = "stranger";
        String CONTENT = "content";
        String IMAGE_CACHE_PATH = "image_cache_path";
        String AUDIO_CACHE_PATH = "audio_cache_path";
        String ORIGINAL_IMAGE_URL = "ORIGINAL_IMAGE_URL";
        String AT_IDS = "AT_IDS";
    }

    @Column(name = ColumnName.DURATION)
    private int duration;

    @Column(name = ColumnName.SEND_STATUS)
    private int sendStatus = 0; // 0发送中 1发送成功 2发送失败

    @Column(name = ColumnName.PLAYED)
    private boolean played = false; // 语音是否播放

    @Column(name = ColumnName.READ)
    private boolean read = false; // 是否已读

    @Column(name = ColumnName.CONTENT)
    private String content = "";

    /**
     * loginUserId_userId 或 loginUserId_groupChatId
     */
    @Column(name = ColumnName.CHAT_ID)
    private String chatId;

    @Column(name = ColumnName.STRANGER)
    @SerializedName("is_stranger")
    private boolean stranger;

    @Column(name = ColumnName.IMAGE_CACHE_PATH)
    private String imageCachePath = "";

    @Column(name = ColumnName.AUDIO_CACHE_PATH)
    private String audioCachePath = "";

    private ArrayList<Integer> at_ids;

    @Column(name = ColumnName.AT_IDS)
    private String atIds;

    @SerializedName("data")
    private MessageData messageData;

    @Column(name = ColumnName.ORIGINAL_IMAGE_URL)
    @SerializedName("url")
    private String originalImageUrl;

    private int w, h;

    public ChatMessage() {
    }

    /**
     * 发送消息
     *
     * @param chat    会话（用户或群聊）
     * @param sender  发送人信息
     * @param content 内容
     */
    public ChatMessage(Chat chat, UserInfo sender, MessageType messageType, String content) {

        int recipientId = chat.getRecipientId();
        int senderId = sender.getId();

        setContent(content);
        setChatId(chat.getChatId());

        switch (chat.getChatType()) {
            case GROUP_CHAT:
                setGroupChatId(recipientId);
                break;
            case PRIVATE_CHAT:
            case STRANGER:
                ArrayList<ContactUser> contactUsers = chat.getContactUsers();
                if (contactUsers.size() > 0) {
                    setRecipientRoleId(contactUsers.get(0).getRole_id());
                }
                setRecipientId(recipientId);
                break;
        }

        setSenderId(senderId);
        setSenderRoleId(sender.getRole_id());
        setUsername(sender.getUsername());
        setUser_img(sender.getUserimg());

        long currentTimeMillis = System.currentTimeMillis();
        setTime(currentTimeMillis);
        setTag(currentTimeMillis);
        setMsgId(sender.getRole_id() + "_" + senderId + "_" + currentTimeMillis + "_" + (new Random().nextInt(10000 - 1000) + 1000 + 1));
//        setMsgId(senderId + "_" + currentTimeMillis + "_" + (new Random().nextInt(10000 - 1000) + 1000 + 1));
        setMessageType(messageType);
    }

    public ChatMessage(Contact recipient, UserInfo sender, MessageType messageType) {
        this(recipient, sender, messageType, "");

    }

    public ChatMessage(Contact recipient, UserInfo sender, MessageType messageType, String content) {
        int recipientId = recipient.getId();
        int senderId = sender.getId();

        if (recipient instanceof ContactUser) {
            setRecipientRoleId(recipient.getRole_id());
            setRecipientId(recipientId);
            setChatId(senderId + "_" + recipientId + "_" + recipient.getRole_id());
        } else if (recipient instanceof ContactGroup) {
            setGroupChatId(recipientId);
            setChatId(senderId + "_" + recipientId);
        }

        setSenderId(senderId);
        setSenderRoleId(sender.getRole_id());
        setUsername(sender.getUsername());
        setUser_img(sender.getUserimg());

        long currentTimeMillis = System.currentTimeMillis();
        setTime(currentTimeMillis);
        setTag(currentTimeMillis);
        setMsgId(sender.getRole_id() + "_" + senderId + "_" + currentTimeMillis + "_" + (new Random().nextInt(10000 - 1000) + 1000 + 1));
//        setMsgId(senderId + "_" + currentTimeMillis + "_" + (new Random().nextInt(10000 - 1000) + 1000 + 1));
        setMessageType(messageType);

        setContent(content);
    }

    public String getChatId() {
        return chatId;
    }

    public void setChatId(String chatId) {
        this.chatId = chatId;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public int getSendStatus() {
        return sendStatus;
    }

    public void setSendStatus(int sendStatus) {
        this.sendStatus = sendStatus;
    }

    public ChatMessageStatus getChatMessageStatus() {
        return ChatMessageStatus.getSendStatus(sendStatus);
    }

    public void setChatMessageStatus(ChatMessageStatus chatMessageStatus) {
        this.sendStatus = chatMessageStatus.getType();
    }

    public boolean isPlayed() {
        return played;
    }

    public void setPlayed(boolean played) {
        this.played = played;
    }

    public boolean isRead() {
        return read;
    }

    public void setRead(boolean read) {
        this.read = read;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public boolean isStranger() {
        return stranger;
    }

    public void setStranger(boolean stranger) {
        this.stranger = stranger;
    }

    public String getImageCachePath() {
        return imageCachePath;
    }

    public void setImageCachePath(String imageCachePath) {
        this.imageCachePath = imageCachePath;
    }

    public String getAudioCachePath() {
        return audioCachePath;
    }

    public void setAudioCachePath(String audioCachePath) {
        this.audioCachePath = audioCachePath;
    }

    public MessageData getMessageData() {
        return messageData;
    }

    public <T extends MessageData> T getMessageData(Class<T> c) {
        return (T) messageData;
    }

    public void setMessageData(MessageData messageData) {
        this.messageData = messageData;
    }

    private Contact contact;

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public Contact getContact() {
        return contact;
    }

    public ArrayList<Integer> getAt_ids() {
        return at_ids == null ? new ArrayList<Integer>() : at_ids;
    }

    public void setAt_ids(ArrayList<Integer> at_ids) {
        this.at_ids = at_ids;
    }

    public String getOriginalImageUrl() {
        return originalImageUrl;
    }

    public void setOriginalImageUrl(String originalImageUrl) {
        this.originalImageUrl = originalImageUrl;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public String getAtIds() {
        return atIds;
    }

    public void setAtIds(String atIds) {
        this.atIds = atIds;
    }

}


